<template>
  <div class="container">
    <van-nav-bar
      title="收货地址"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-address-list
      v-model="chosenAddressId"
      :list="list"
      default-tag-text="默认"
      @add="onAdd"
      @edit="onEdit"
      @select="onSelect"
    />
  </div>
</template>

<script>
import { getUserReceiving } from "../../api/api";
export default {
  data() {
    return {
      chosenAddressId: "1",
      list: [],
    };
  },
  methods: {
    onAdd() {
      this.$router.push("/addressEdit");
    },
    onEdit(item, index) {
      this.$router.push({
        path: "/addressEdit",
        query: {
          item: JSON.stringify(item),
        },
      });
      console.log("编辑地址---", item, index);
    },
    onClickLeft() {
      this.$router.push("/index/core");
    },
    onSelect(item, index) {
      console.log("切换地址---", item, index);
      localStorage.setItem('Receiving', JSON.stringify(item))
    },
    getUserList() {
      const data = {
        user_id: localStorage.getItem('userId')
      }
      getUserReceiving(data).then(res => {
        this.$toast.clear()
        this.list = res.data
        this.list.forEach(item => {
          item.tel = item.mobile
          item.address = `${item.province}${item.city}${item.district}`
          item.county = item.district
          item.addressDetail = item.address
          item.isDefault = item.checked == 1 ? true : false
          if(item.checked == 1) localStorage.setItem('Receiving', JSON.stringify(item))
        })
        console.log('收获地址列表---', res)
      })
    }
  },
  mounted() {
    this.getUserList()
  },
};
</script>